package com.oncliedisk.administrator.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.oncliedisk.administrator.DB.Dao;
import com.oncliedisk.administrator.content.OnlineConfig;
import com.oncliedisk.administrator.interfaces.OnActionClickListener;
import com.oncliedisk.administrator.model.FileTaskModel;
import com.oncliedisk.administrator.onlinedisk.R;
import com.oncliedisk.administrator.utils.HttpUtils;
import com.oncliedisk.administrator.utils.ImageUtils;

import java.util.List;

/**
 * Created by Administrator on 2015/12/17.
 */
public class TaskAdapter extends BaseAdapter {
    private List<FileTaskModel> data;
    private Context context;
    private LayoutInflater inflater;
    private boolean titleDown;
    private boolean titleFinish;
    private OnActionClickListener onActionClickListener;

    public TaskAdapter(Context context, List<FileTaskModel> data, OnActionClickListener onActionClickListener) {
        this.context = context;
        this.data = data;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.onActionClickListener = onActionClickListener;
    }

    public List<FileTaskModel> getData() {
        titleDown = false;
        titleFinish = false;
        return data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_task, null);
            holder = new ViewHolder();
            holder.head = (ImageView) convertView.findViewById(R.id.task_head);
            holder.action = (ImageView) convertView.findViewById(R.id.task_action);
            holder.name = (TextView) convertView.findViewById(R.id.task_name);
            holder.currSize = (TextView) convertView.findViewById(R.id.task_curr_progress);
            holder.fileSize = (TextView) convertView.findViewById(R.id.task_fileSize);
            holder.state = (TextView) convertView.findViewById(R.id.task_state);
            holder.bar = (ProgressBar) convertView.findViewById(R.id.task_progress);
            holder.title = (TextView) convertView.findViewById(R.id.task_title);
            holder.xg = (TextView) convertView.findViewById(R.id.task_g);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final FileTaskModel model = data.get(position);
        holder.title.setVisibility(View.GONE);
        //设置名字
        holder.name.setText(model.getFileName());
        //设置文件大小
        holder.fileSize.setText(model.getSize());
        //根据类型进行选择图标
        holder.head.setImageResource(ImageUtils.getImageByType(model.getFileName()));
        holder.bar.setMax(Integer.valueOf(100));
        if(TextUtils.isEmpty(model.getProgress())){
            holder.bar.setProgress(0);
        }else{
            holder.bar.setProgress(Integer.valueOf(model.getProgress()));
        }
        holder.currSize.setText(model.getCurrSize());
//        holder.bar.setProgress(0);
        //根据状态执行指定状态的操作
        int state = model.getState();
        switch (state) {
            case FileTaskModel.STATE_WAIT_DOWN:
                //如果没有在开启线程加载东西，就可以进行加载
                break;
            case FileTaskModel.STATE_DOWN_FAIL:
                //下载失败
                holder.state.setVisibility(View.VISIBLE);
                holder.state.setText("下载失败");
                break;
            case FileTaskModel.STATE_DOWN_STOP:
                //下载暂停
                holder.bar.setVisibility(View.VISIBLE);
                holder.state.setText("暂停传输");
                holder.action.setVisibility(View.VISIBLE);
                holder.currSize.setVisibility(View.VISIBLE);
                if(TextUtils.isEmpty(model.getCurrSize())){
                    holder.currSize.setText("0 kb");
                }
                holder.action.setImageResource(R.drawable.down_img);
                break;
            case FileTaskModel.STATE_DOWN_SUCCESS:
                //如果下載成功
                holder.currSize.setVisibility(View.VISIBLE);
                holder.fileSize.setVisibility(View.VISIBLE);
                holder.currSize.setText(model.getSize());
                holder.fileSize.setText(model.getDate());
                holder.action.setVisibility(View.INVISIBLE);
                if (model.getTaskType() == OnlineConfig.TYPE_DOWN) {
                    holder.state.setText("下载成功");
                } else {
                    holder.state.setText("上传成功");
                }
//                holder.state.setVisibility(View.GONE);
                holder.bar.setVisibility(View.GONE);
                holder.xg.setVisibility(View.GONE);
                if(titleFinish){
                    holder.title.setVisibility(View.GONE);
                }
                TaskAdapter.this.notifyDataSetChanged();
                break;
            case FileTaskModel.STATE_DOWNING:
                //正在下载
                holder.state.setVisibility(View.VISIBLE);
                holder.state.setText("正在传输");
                holder.currSize.setVisibility(View.VISIBLE);
                if (model.getCurrSize() == null || model.getCurrSize().equals("")) {
                    holder.currSize.setText("0 kb");
                } else {
                    holder.currSize.setText(model.getCurrSize());
                }
                holder.bar.setVisibility(View.VISIBLE);
                if(TextUtils.isEmpty(model.getProgress())){
                    holder.bar.setProgress(0);
                }else{
                    holder.bar.setProgress(Integer.valueOf(model.getProgress()));
                }

                holder.action.setVisibility(View.VISIBLE);
                holder.action.setImageResource(R.drawable.shop_img);
                break;
        }
        //根据位置，是否显示头部的标题
        if (!titleDown) {
            if (model.getState() == FileTaskModel.STATE_DOWNING || model.getState() == FileTaskModel.STATE_DOWN_STOP) {
                holder.title.setVisibility(View.VISIBLE);
                if (model.getTaskType() .equals(OnlineConfig.TYPE_DOWN)) {
                    holder.title.setText("正在下载(" + getWaitData(data, FileTaskModel.STATE_DOWNING,FileTaskModel.getStateDownStop()) + ")");
                } else {
                    holder.title.setText("正在上传(" + getWaitData(data, FileTaskModel.STATE_DOWNING,FileTaskModel.getStateDownStop()) + ")");
                }
                titleDown = true;
            }
        }
        if (!titleFinish) {
            if (model.getState() == FileTaskModel.STATE_DOWN_SUCCESS) {
                holder.title.setVisibility(View.VISIBLE);
                if (model.getTaskType().equals(OnlineConfig.TYPE_DOWN)) {
                    holder.title.setText("下载完成(" + getWaitData(data, FileTaskModel.STATE_DOWN_SUCCESS,0) + ")");
                } else {
                    holder.title.setText("上传完成(" + getWaitData(data, FileTaskModel.STATE_DOWN_SUCCESS,0) + ")");
                }
                titleFinish = true;
            }
        }
        final ViewHolder finalHolder = holder;
        holder.action.setOnClickListener((View.OnClickListener) new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int type = 0;
                if (model.getState() == FileTaskModel.STATE_DOWNING) {
                    //如果是你下载中点击，就让你停止
                    model.setState(FileTaskModel.STATE_DOWN_STOP);
                    Dao.getInstance(context).updateState(FileTaskModel.STATE_DOWN_STOP, null, model);
                    type = 1;
                    finalHolder.action.setImageResource(R.drawable.down_img);
                } else if (model.getState() == FileTaskModel.STATE_DOWN_STOP) {
                    if(!HttpUtils.isNetworkAvailable(context)){
                        Toast.makeText(context, "请检查网络状态是否可用!", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    model.setState(FileTaskModel.STATE_DOWNING);
                    Dao.getInstance(context).updateState(FileTaskModel.STATE_DOWNING, null, model);
                    type = 2;
                    finalHolder.action.setImageResource(R.drawable.shop_img);
                }
                TaskAdapter.this.notifyDataSetChanged();
                onActionClickListener.onActionClick(model, type);
            }
        });
        return convertView;
    }

    /**
     * 获取等待下载的有几个
     *
     * @param data
     * @return
     */
    public int getWaitData(List<FileTaskModel> data, int type,int outType) {
        int index = 0;
        for (FileTaskModel model : data) {
            if (model.getState() == type) {
                index++;
            }
            if(outType != 0){
                if(model.getState() == outType){
                    index++;
                }
            }
        }
        return index;
    }

    public class ViewHolder {
        ImageView head;
        ImageView action;
        TextView name;
        TextView currSize;
        TextView fileSize;
        TextView state;
        TextView title;
        TextView xg;
        ProgressBar bar;
    }

}
